package com.nisco.dms.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.nisco.dms.entity.Department;
import com.nisco.dms.repository.mybatis.DeptMyBatisDao;

/**
 * 
 * @author joey
 * 
 */
@Component
@Transactional(readOnly = true)
public class DeptService {
	@Autowired
	private DeptMyBatisDao deptMyBatisDao;

	public List<Department> getAll() {
		return deptMyBatisDao.getAll();
	}
	
	public List<Department> getAllByPage(int pageNow,int pageSize) {
		Map<String, Object> parameters = Maps.newHashMap();
		parameters.put("start", pageNow);
		parameters.put("end", pageSize);
		return deptMyBatisDao.getAllByPage(parameters);
	}
	public List<Department> search(String deptcode, String deptname) {
		Map<String, Object> parameters = Maps.newHashMap();
		if (deptcode == null || deptcode == "") {
			parameters.put("deptname", deptname);
			parameters.put("deptcode", null);
		} else if (deptname == null || deptname == "") {
			parameters.put("deptname", null);
			parameters.put("deptcode", deptcode);
		} else {
			parameters.put("deptname", deptname);
			parameters.put("deptcode", deptcode);
		}

		return deptMyBatisDao.search(parameters);
	}

	
	public List<Department> search(String param) {
		Map<String, Object> parameters = Maps.newHashMap();
		parameters.put("deptname", param);
		parameters.put("deptcode", param);
		parameters.put("description", param);
		return deptMyBatisDao.search(parameters);
	}
	
	public List<Department> search(String param,int pageNow,int pageSize) {
		Map<String, Object> parameters = Maps.newHashMap();
		parameters.put("deptname", param);
		parameters.put("deptcode", param);
		parameters.put("description", param);
		parameters.put("start", pageNow);
		parameters.put("end", pageSize);
		return deptMyBatisDao.searchByPage(parameters);
	}

	public Department get(String id) {
		return deptMyBatisDao.get(id);
	}

	@Transactional
	public void save(Department dept) {
		deptMyBatisDao.save(dept);
	}

	@Transactional
	public void delete(String id) {
		deptMyBatisDao.delete(id);
	}

	@Transactional
	public void update(Department dept) {
		deptMyBatisDao.update(dept);
	}
}
